﻿using System;
using System.Collections.Generic;
using System.Text;
using GroupChat2013.Common;
using GroupChat2013.Interface;
using GroupChat2013.Logic;
using GroupChat2013.Logic.Interface;

namespace GroupChat2013.Main.Components
{
    /// <summary>
    /// 更新用户离线状态处理
    /// </summary>
    public class UserOfflineProcess : IReceiveProcess
    {
        private FrmMain frmMain;

        /// <summary>
        /// 消息处理标志
        /// </summary>
        private string key;

        private string userInfoId = "userInfoId";

        /// <summary>
        /// 构造函数
        /// </summary>
        public UserOfflineProcess(FrmMain frmMain)
        {
            this.frmMain = frmMain;
            key = ConstClass.GETUSEROFFLINENOTIFY;
        }

        /// <summary>
        /// 消息处理
        /// </summary>
        /// <param name="message"></param>
        /// <returns></returns>
        public bool Process(Entity.Protocol.TransferMsgInfo message)
        {
            if (!message.action.Equals(key))
                return false;

            if (message.data.ContainsKey(userInfoId))
            {
                string userId = message.data[userInfoId];
                frmMain.UpdateUserStatus(userId, CCWin.SkinControl.ChatTreeSubItem.UserStatus.OffLine);

                foreach (IChat chat in EngineContext.Current.ChatManager.GetChats())
                {
                    if (chat != null && chat is IGroupChat)
                    {
                        (chat as IGroupChat).UpdateUserStatus(userId, 0);
                    }
                }
                
                return true;
            }

            return false;
        }
    }
}
